<template>
  <div class="addDiary">
    <el-input v-model="diary.title" placeholder="这里是标题..."
     style="width: 600px;margin-left: 10px;">
    </el-input>
    <hr></hr>
    <el-input v-model="diary.content" placeholder="请在此输入日记内容..." clearable :autosize="{ minRows: 10, maxRows: 50}"
      maxlength="5000" show-word-limit type="textarea">
    </el-input>
    <el-button @click = "cancleEdit" style="margin-top: 20px;margin-left: 60px;">放弃修改</el-button>
    <el-button @click = "saveDiary" style="margin-top: 20px;margin-right: 60px;">保存日记</el-button>
  </div>
</template>

<script>
  import axios from 'axios'

  name: "addDiary"
  export default{
    data(){
      return{
        diary:{
          title:"",
          content:"",
          editTime:"",
          uid:"",
          state:""
        }
      }
    },
    mounted: function(){
      var _this = this;

    },
    methods:{
      cancleEdit(){
        this.$router.push('/timeline');
      },
      saveDiary(){
        var _this = this;
        if(this.$data.diary.title == "" || this.$data.diary.content == ""){
          alert("标题与正文不能为空！")
        }else{
          //向后端发送数据
          axios.post('http://abcds.vaiwan.com/new', {
            title: this.$data.diary.title,
            content: this.$data.diary.content,
            uid:"2",
              })
              .then(function(response){
                //成功时返回response数据
                /*
                did:""
                state:"2"
                eiditTime:""
                */
               _this.diary.did = response.data.did;
               _this.diary.state = response.data.state;
               alert("发表成功！");
               _this.$router.replace("/timeline");
              })
              .catch(function(error){
                console.log(error);
                alert("发表失败！");
              });
        }
      }
    }
  }
</script>

<style>
</style>
